﻿// ***********************************************************************
// Provided for Informational Purposes Only
//
// Apache 2.0 License
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY 
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR 
// PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.
// ***********************************************************************
// Assembly  : ContractMeow.Api.WinRT
// File      : UserClient.cs
//
// ***********************************************************************
using ContractMeow.Api.Dtos;
using ContractMeow.Api.Dtos.User;
using ContractMeow.WinRT.Serialization;
using System;
using System.Net;
using System.Threading.Tasks;

namespace ContractMeow.Api.Clients
{
	/// <summary>
	/// Provides access to the message-related endpoints in the Yammer API
	/// .../api/v1/users/
	/// </summary>
	public class UserClient : ClientBase, IUserClient
	{
		#region Resources

		/// <summary>
		/// Get user by id resource
		/// </summary>
		private const string GetByIdResource = "/api/v1/users/{0}.json";

		/// <summary>
		/// Get current Resource
		/// </summary>
		private const string GetCurrentResource = "/api/v1/users/current.json";

		#endregion

		#region Ctors

		/// <summary>
		/// Initializes a new instance of the <see cref="UserClient" /> class.
		/// </summary>
		/// <param name="configuration">The configuration.</param>
		/// <param name="queryStringSerializer">The query string serializer.</param>
		/// <param name="serializer">The serializer.</param>
		/// <param name="deserializer">The deserializer.</param>
		/// <param name="responseErrorHandler">The response error handler.</param>
		/// <param name="proxy">The proxy.</param>
		public UserClient(IClientConfiguration configuration,
						  IQueryStringSerializer queryStringSerializer,
						  ISerializer serializer,
						  IDeserializer deserializer,
						  IResponseErrorHandler responseErrorHandler,
						  ICookieStore cookieStore,
						  IWebProxy proxy = null)
			: base(configuration, queryStringSerializer, serializer, deserializer, responseErrorHandler, cookieStore, proxy) { }

		#endregion

		/// <summary>
		/// Gets the current user asynchronously.
		/// </summary>
		/// <param name="principal">The principal.</param>
		/// <param name="requestParameters">The request parameters.</param>
		/// <returns>The <see cref="User" /> representation for the current logged in user.</returns>
		/// <exception cref="System.Exception">Client is not logged in.</exception>
		public async Task<User> GetCurrentUserAsync(Principal principal, UserRequestParameters requestParameters = null)
		{
			VerifyCredentials(principal);
			requestParameters = requestParameters ?? new UserRequestParameters();

			var r = await GetAsync<User>(GetCurrentResource, principal, requestParameters.ToApiParameters());
			return r.Result();
		}

		/// <summary>
		/// Gets the user by id asynchronously.
		/// </summary>
		/// <param name="principal">The principal.</param>
		/// <param name="id">The id.</param>
		/// <returns>Task{User}.</returns>
		/// <exception cref="System.Exception">Client is not logged in.</exception>
		public async Task<User> GetUserByIdAsync(Principal principal, Int64 id)
		{
			VerifyCredentials(principal);

			var r = await GetAsync<User>(String.Format(GetByIdResource, id), principal);
			return r.Result();
		}
	}
}